﻿Imports Crystal.CLL
Imports CrystalDataHub.BLL
Imports System.Data.SqlClient
Imports CrystalDataHub.BLL.CrystalDataHub.BLL

Public Class ImportSummary
    Inherits BaseForm

#Region "Fields"

    Private mclsImporteSourceManager As New ImporteSourceManager

    Private mintDoubtCount As Integer
    Private mintBothUnmappedCount As Integer
    Private mintFundMappedCount As Integer
    Private mintInvestorMappedCount As Integer
    Private mintBothMappedCount As Integer
    Public menmImportSummaryType As ImportSummaryType
    Public menmSourceTypeCode As SourceTypeCode
    Public mstrExcelFundField As String = ""

    Public mstrMappedInvName As String
    Public mstrMappedFundName As String
    Public mintProjectID As Integer
    Public mintListingID As Integer
    Public mintEntityID As Integer
    Public mintClientID As Int16
    Public mintTemplateID As Int16

    Public mstrMatchCol1 As String
    Public mstrMatchCol2 As String
    Public mstrMatchCol3 As String
    Public mstrMatchCol4 As String
    Public mstrMatchCol5 As String
    Public mstrPosition As String
    
    Public mdtColMapping As New DataTable
    Public marrMatchCols() As String

    Public mdtMapData As New DataTable
    Public mblnIsImportSuccess As Boolean = False

    Private FirstPanelLocation As Point
    Private SecondPanelLocation As Point
    Private ThirdPanelLocation As Point
    Private FourthPanelLocation As Point
    Private FifthPanelLocation As Point
    Private mstrScreenName As String = "Import Summary"
#End Region

#Region "Constructors"
    Sub New(ByVal pintDoubtCount As Integer, ByVal pintBothUnmappedCount As Integer, ByVal pintFundMappedCount As Integer, ByVal pintInvestorMappedCount As Integer, ByVal pintBothMappedCount As Integer)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        mintDoubtCount = pintDoubtCount
        mintBothUnmappedCount = pintBothUnmappedCount
        mintFundMappedCount = pintFundMappedCount
        mintInvestorMappedCount = pintInvestorMappedCount
        mintBothMappedCount = pintBothMappedCount
    End Sub
#End Region

#Region "On Load"

    Private Sub ImportSummary_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CallOnLoad()
        SetControlValues()
    End Sub

    Private Sub CallOnLoad()
        SetOnLoadControls()
    End Sub

    Public Sub SetOnLoadControls()
        Me.Text = TitleManager.GetTitle("ImportSummary")
        lblDoubtNotImported.Visible = False
        lblBothUnmappedNotImported.Visible = False
        lblFundMappedNotImported.Visible = False
        lblInvMappedImported.Visible = False
        lblImportProgress.Visible = False

        pnlDoubts.Visible = False
        pnlBothUnmapped.Visible = False
        pnlFundMapped.Visible = False
        pnlInvMapped.Visible = False
        pnlBothMapped.Visible = False
        pgbImport.Visible = True
        pgbImport.Step = 20
        pgbImport.Maximum = 100

        FirstPanelLocation = New Point(pnlDoubts.Location.X, pnlDoubts.Location.Y)
        SecondPanelLocation = New Point(pnlBothUnmapped.Location.X, pnlBothUnmapped.Location.Y)
        ThirdPanelLocation = New Point(pnlFundMapped.Location.X, pnlFundMapped.Location.Y)
        FourthPanelLocation = New Point(pnlInvMapped.Location.X, pnlInvMapped.Location.Y)
        FifthPanelLocation = New Point(pnlBothMapped.Location.X, pnlBothMapped.Location.Y)
    End Sub

#End Region

#Region "Events"

#Region "Link Button Events"
    ''' <summary>
    ''' Set the controls visibility and location
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lnkDoubtNo_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkDoubtNo.LinkClicked
        lblDoubtNotImported.Visible = True
        SetDoubtChidrenPanels()
    End Sub

    ''' <summary>
    ''' Set the controls visibility and location
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lnkBothUnmappedNo_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkBothUnmappedNo.LinkClicked
        lblBothUnmappedNotImported.Visible = True
        SetBothUnmappedChidrenPanels()
    End Sub

    ''' <summary>
    ''' Set the controls visibility and location
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lnkInvUnmappedNo_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkFundMappedNo.LinkClicked
        lblFundMappedNotImported.Visible = True
        SetFundMappedChidrenPanels()
    End Sub

    ''' <summary>
    ''' Set the controls visibility and location
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lnkFundUnmappedNo_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkInvMappedNo.LinkClicked
        lblInvMappedImported.Visible = True
        SetInvMappedChidrenPanels()
    End Sub

    ''' <summary>
    ''' To show the doubts in Import EDS Screen
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lnkDoubtYes_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkDoubtYes.LinkClicked
        menmImportSummaryType = ImportSummaryType.Doubt
        Me.Close()
    End Sub

    ''' <summary>
    ''' To show the doubts in Import EDS Screen
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lnkBothUnmappedYes_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkBothUnmappedYes.LinkClicked
        menmImportSummaryType = ImportSummaryType.BothUnmapped
        Me.Close()
    End Sub

    ''' <summary>
    ''' To show the doubts in Import EDS Screen
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lnkInvUnmappedYes_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkFundMappedYes.LinkClicked
        menmImportSummaryType = ImportSummaryType.FundMapped
        Me.Close()
    End Sub

    ''' <summary>
    ''' To show the doubts in Import EDS Screen
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lnkFundUnmappedYes_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkInvMappedYes.LinkClicked
        menmImportSummaryType = ImportSummaryType.InvestorMapped
        Me.Close()
    End Sub

#End Region

#End Region

#Region "Procedures"
    ''' <summary>
    ''' Set the relevant panel visibility
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetDoubtVisibility()
        If mintDoubtCount > 0 Then
            lblDoubtNumber.Text = mintDoubtCount.ToString
            pnlDoubts.Visible = True
            pgbImport.PerformStep()
        Else
            pnlDoubts.Visible = False
        End If
    End Sub

    ''' <summary>
    ''' Set the relevant panel visibility
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetBothUnmappedVisibility()
        If mintBothUnmappedCount > 0 Then
            lblBothUnmappedNumber.Text = mintBothUnmappedCount.ToString
            pnlBothUnmapped.Visible = True
            If pnlDoubts.Visible = False Then
                pnlBothUnmapped.Location = FirstPanelLocation
            End If
            pgbImport.PerformStep()
        Else
            pnlBothUnmapped.Visible = False
        End If
    End Sub

    ''' <summary>
    ''' Set the relevant panel visibility
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetFundMappedVisibility()
        If mintFundMappedCount > 0 Then
            lblFundMappedNumber.Text = mintFundMappedCount.ToString
            pnlFundMapped.Visible = True
            If pnlDoubts.Visible = False OrElse pnlBothUnmapped.Visible = False Then
                pnlFundMapped.Location = SecondPanelLocation
            End If
            pgbImport.PerformStep()
        Else
            pnlFundMapped.Visible = False
        End If
    End Sub

    ''' <summary>
    ''' Set the relevant panel visibility
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetInvMappedVisibility()
        If mintInvestorMappedCount > 0 Then
            lblInvMappedNumber.Text = mintInvestorMappedCount.ToString
            pnlInvMapped.Visible = True
            Dim lintVisiblePanelsCount As Int16 = 0
            If pnlDoubts.Visible Then
                lintVisiblePanelsCount += 1
            End If
            If pnlBothUnmapped.Visible Then
                lintVisiblePanelsCount += 1
            End If
            If pnlFundMapped.Visible Then
                lintVisiblePanelsCount += 1
            End If
            If lintVisiblePanelsCount = 0 Then
                pnlInvMapped.Location = FirstPanelLocation
            ElseIf lintVisiblePanelsCount = 1 Then
                pnlInvMapped.Location = SecondPanelLocation
            ElseIf lintVisiblePanelsCount = 2 Then
                pnlInvMapped.Location = ThirdPanelLocation
            End If
            pgbImport.PerformStep()
        Else
            pnlInvMapped.Visible = False
        End If
    End Sub

    ''' <summary>
    ''' Set the relevant panel visibility
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetBothMappedVisibility()
        If mintBothMappedCount > 0 Then
            lblBothMappedNumber.Text = mintBothMappedCount.ToString
            pnlBothMapped.Visible = True
            Dim lintVisiblePanelsCount As Int16 = 0
            If pnlDoubts.Visible Then
                lintVisiblePanelsCount += 1
            End If
            If pnlBothUnmapped.Visible Then
                lintVisiblePanelsCount += 1
            End If
            If pnlFundMapped.Visible Then
                lintVisiblePanelsCount += 1
            End If
            If pnlInvMapped.Visible Then
                lintVisiblePanelsCount += 1
            End If
            If lintVisiblePanelsCount = 0 Then
                pnlBothMapped.Location = FirstPanelLocation
            ElseIf lintVisiblePanelsCount = 1 Then
                pnlBothMapped.Location = SecondPanelLocation
            ElseIf lintVisiblePanelsCount = 2 Then
                pnlBothMapped.Location = ThirdPanelLocation
            ElseIf lintVisiblePanelsCount = 3 Then
                pnlBothMapped.Location = FourthPanelLocation
            End If
        Else
            pnlBothMapped.Visible = False
        End If
        ImportESource()
    End Sub

    Private Sub SetControlValues()
        SetDoubtVisibility()
        If pnlDoubts.Visible = False Then
            SetBothUnmappedVisibility()
            If pnlBothUnmapped.Visible = False Then
                SetFundMappedVisibility()
                If pnlInvMapped.Visible = False Then
                    SetInvMappedVisibility()
                    If pnlFundMapped.Visible = False Then
                        SetBothMappedVisibility()
                    End If
                End If
            End If
        End If
    End Sub

    ''' <summary>
    ''' Set the other downward panel's visibility
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetDoubtChidrenPanels()
        SetBothUnmappedVisibility()
        If pnlBothUnmapped.Visible = False Then
            SetBothUnmappedChidrenPanels()
        End If
    End Sub

    ''' <summary>
    ''' Set the other downward panel's visibility
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetBothUnmappedChidrenPanels()
        SetFundMappedVisibility()
        If pnlFundMapped.Visible = False Then
            SetFundMappedChidrenPanels()
        End If
    End Sub

    ''' <summary>
    ''' Set the other downward panel's visibility
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetFundMappedChidrenPanels()
        SetInvMappedVisibility()
        If pnlInvMapped.Visible = False Then
            SetInvMappedChidrenPanels()
        End If
    End Sub

    ''' <summary>
    ''' Set the other downward panel's visibility
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetInvMappedChidrenPanels()
        SetBothMappedVisibility()
    End Sub

    Private Sub ImportESource()
        Dim lstrQuery As String
        Dim larrDBCol() As DataRow
        Dim larrMapData() As DataRow
        Dim ldtImport As DataTable = Nothing
        Dim sqlBulkCopy As SqlBulkCopy = Nothing
        Dim larrSameFundInNewInv() As DataRow
        Try
            'Disable the controls
            lblImportProgress.Visible = True
            Me.Cursor = Cursors.WaitCursor
            DisableControls(Me.Controls, Nothing)
            lnkBothUnmappedNo.Enabled = False
            lnkBothUnmappedYes.Enabled = False
            lnkDoubtNo.Enabled = False
            lnkDoubtYes.Enabled = False
            lnkFundMappedNo.Enabled = False
            lnkFundMappedYes.Enabled = False
            lnkInvMappedNo.Enabled = False
            lnkInvMappedYes.Enabled = False
            Application.DoEvents()
            'Temporarely commened.
            'To deduct/add position from Investor positions, if the fund is shifted or fund is unmapped
            'If menmSourceTypeCode = SourceTypeCode.PBC Then
            '    Dim lintInvestorRowIndex As Integer
            '    Dim lintLastFundRowIndex As Integer
            '    Dim lintCurrentRowIndex As Integer

            '    Dim lstrInvDBCol As String
            '    Dim lstrInvExcelCol As String
            '    Dim lstrFundDBCol As String
            '    Dim lstrFundExcelCol As String

            '    'If MatchCol1 is not blank, then set the Investor EntityID's Database and Excel column name
            '    If mstrMatchCol1 <> "" Then
            '        lstrInvDBCol = PublicMapColumns.InvEntityID.ToString
            '        lstrInvExcelCol = mstrMatchCol1
            '    Else
            '        lstrInvDBCol = mstrMappedInvName
            '        'lstrInvDBCol = PublicMapColumns.MappedInvestorName.ToString
            '        lstrInvExcelCol = mstrMatchCol3
            '    End If

            '    'If MatchCol1 is not blank, then set the Fund EntityID's Database and Excel column name
            '    If mstrMatchCol2 <> "" Then
            '        lstrFundDBCol = PublicMapColumns.FundEntityID.ToString
            '        lstrFundExcelCol = mstrMatchCol2
            '    Else
            '        lstrFundDBCol = mstrMappedFundName
            '        'lstrFundDBCol = PublicMapColumns.MappedFundName.ToString
            '        lstrFundExcelCol = mstrMatchCol4
            '    End If

            '    For Each lRow As DataRow In mdtMapData.Rows
            '        'Dim lint As Integer = mdtMapData.Rows.IndexOf(lRow)
            '        'If lint = 97 Then
            '        '    MessageBox.Show("")
            '        'End If
            '        'If an Investor for a fund is changed and fund is not blank. Deduct the fund's positions from old investor and add them in new investor.
            '        If lRow.Item(lstrInvExcelCol).ToString <> lRow.Item(lstrInvDBCol).ToString AndAlso String.IsNullOrEmpty(lRow.Item(lstrInvDBCol).ToString) = False AndAlso String.IsNullOrEmpty(lRow.Item(lstrFundDBCol).ToString) = False Then
            '            lintCurrentRowIndex = mdtMapData.Rows.IndexOf(lRow)
            '            lstrQuery = "[" + lstrInvDBCol + "]='" + lRow.Item(lstrInvDBCol).ToString + "' AND [" + lstrFundDBCol + "] IS NULL AND [" + lstrFundExcelCol + "] IS NULL"
            '            larrMapData = mdtMapData.Select(lstrQuery)
            '            If larrMapData.Length >= 1 Then
            '                lintInvestorRowIndex = mdtMapData.Rows.IndexOf(larrMapData(0))
            '                lintLastFundRowIndex = lintInvestorRowIndex + 1
            '                'Deduction & Addition has to perform if it's not in the consecutive list of funds of that investor. This is required, because the same fund might exist under the newly selected investor.
            '                While lintLastFundRowIndex < mdtMapData.Rows.Count
            '                    If mdtMapData.Rows(lintLastFundRowIndex).Item(lstrInvDBCol).ToString <> larrMapData(0).Item(lstrInvDBCol).ToString Then
            '                        Exit While
            '                    End If
            '                    lintLastFundRowIndex += 1
            '                End While
            '                If lintCurrentRowIndex < lintInvestorRowIndex OrElse lintCurrentRowIndex > lintLastFundRowIndex Then
            '                    ''Check if the fund in selection exists under the newly selected investor; If so, then deduct the existing fund's position from the newly selected investor
            '                    'lstrQuery = "[" + lstrInvDBCol + "]='" + lRow.Item(lstrInvDBCol).ToString + "' AND [" + lstrFundDBCol + "]='" + lRow.Item(lstrFundDBCol).ToString + "'"
            '                    'larrSameFundInNewInv = mdtMapData.Select(lstrQuery)
            '                    'If larrSameFundInNewInv.Length > 0 Then
            '                    '    For Each ldrSameFundRow As DataRow In larrSameFundInNewInv
            '                    '        larrMapData(0).Item(mstrPosition) = Convert.ToInt64(larrMapData(0).Item(mstrPosition)) - Convert.ToInt64(ldrSameFundRow.Item(mstrPosition))
            '                    '    Next
            '                    'End If
            '                    'Adding the positions in the newly selected investor
            '                    larrMapData(0).Item(mstrPosition) = Convert.ToInt64(larrMapData(0).Item(mstrPosition)) + Convert.ToInt64(lRow.Item(mstrPosition))
            '                    'Find the old/original investor
            '                    lstrQuery = "[" + lstrInvDBCol + "]='" + lRow.Item(lstrInvExcelCol).ToString + "' AND [" + lstrFundDBCol + "] IS NULL"
            '                    larrMapData = mdtMapData.Select(lstrQuery)
            '                    If larrMapData.Length >= 1 Then
            '                        'Deduct the positions from the old investor
            '                        larrMapData(0).Item(mstrPosition) = Convert.ToInt64(larrMapData(0).Item(mstrPosition)) - Convert.ToInt64(lRow.Item(mstrPosition))
            '                        If larrMapData(0).Item(mstrPosition) < 0 Then
            '                            larrMapData(0).Item(mstrPosition) = 0
            '                        End If
            '                    End If
            '                End If
            '                'End If
            '            End If
            '            'If a fund is unmapped; i.e. Investor is not blank but fund is blank, deduct the fund's positions from Investors
            '        ElseIf String.IsNullOrEmpty(lRow.Item(lstrInvDBCol).ToString) = False AndAlso String.IsNullOrEmpty(lRow.Item(lstrInvExcelCol).ToString) = False AndAlso String.IsNullOrEmpty(lRow.Item(lstrFundDBCol).ToString) AndAlso String.IsNullOrEmpty(lRow.Item(lstrFundExcelCol).ToString) = False Then
            '            lstrQuery = "[" + lstrInvDBCol + "]='" + lRow.Item(lstrInvDBCol).ToString + "' AND [" + lstrFundDBCol + "] IS NULL AND [" + lstrFundExcelCol + "] IS NULL"
            '            'lstrQuery = "[" + lstrInvDBCol + "]='" + lRow.Item(lstrInvDBCol).ToString + "' AND [" + lstrFundDBCol + "] IS NULL AND [Excel Mapped Fund Name] IS NULL"
            '            larrMapData = mdtMapData.Select(lstrQuery)
            '            If larrMapData.Length >= 1 Then
            '                lintInvestorRowIndex = mdtMapData.Rows.IndexOf(larrMapData(0))
            '                larrMapData(0).Item(mstrPosition) = Convert.ToInt64(larrMapData(0).Item(mstrPosition)) - Convert.ToInt64(lRow.Item(mstrPosition))
            '                lRow.Item(mstrPosition) = 0
            '                If larrMapData(0).Item(mstrPosition) < 0 Then
            '                    larrMapData(0).Item(mstrPosition) = 0
            '                End If
            '            End If
            '        End If
            '    Next
            '    '''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'End If
            'Clear the staging table
            mclsImporteSourceManager.ClearStagingTable("DE_BulkESourceImport", mintEntityID)

            sqlBulkCopy = New SqlBulkCopy(mdlDataHubBLLCommon.mstrConnectionstring)
            sqlBulkCopy.DestinationTableName = "DE_BulkESourceImport"
            'Specify the mappings to insert into staging table
            sqlBulkCopy.ColumnMappings.Add("RowNumber", "RowNumber")
            sqlBulkCopy.ColumnMappings.Add("InvestorID", "InvestorID")
            sqlBulkCopy.ColumnMappings.Add("Doubts", "Doubt")
            sqlBulkCopy.ColumnMappings.Add("FundID", "FundID")
            sqlBulkCopy.ColumnMappings.Add("ETemplateID", "ETemplateID")
            sqlBulkCopy.ColumnMappings.Add("ClientID", "ClientID")
            sqlBulkCopy.ColumnMappings.Add("UserID", "UserID")

            'Specify mappings for the match columns
            For Each lstrMatch As String In marrMatchCols
                'Setting name value for Match Columns
                lstrQuery = "[Map Field] ='" + lstrMatch + "'"
                larrDBCol = mdtColMapping.Select(lstrQuery)
                If larrDBCol.Length = 1 Then
                    If larrDBCol(0).Item("DBFieldName").ToString <> "" Then
                        If menmSourceTypeCode = SourceTypeCode.PBC Then
                            sqlBulkCopy.ColumnMappings.Add(lstrMatch, larrDBCol(0).Item("PublicMatch"))
                        Else
                            sqlBulkCopy.ColumnMappings.Add(lstrMatch, larrDBCol(0).Item("DBFieldName"))
                        End If
                    End If
                End If
            Next
            Dim lstrSourceNameColName As String = ""
            Dim lstrFilingDateColName As String = ""

            'Specify column mapping for Source and FilingDate for Public
            If menmSourceTypeCode = SourceTypeCode.PBC Then
                larrDBCol = mdtColMapping.Select("DBFieldName ='Source'")
                If larrDBCol.Length = 1 Then
                    If larrDBCol(0).Item("Map Field").ToString <> "" Then
                        lstrSourceNameColName = larrDBCol(0).Item("Map Field").ToString
                        sqlBulkCopy.ColumnMappings.Add(lstrSourceNameColName, "PSource")
                    End If
                End If
                larrDBCol = mdtColMapping.Select("DBFieldName ='FilingDate'")
                If larrDBCol.Length = 1 Then
                    If larrDBCol(0).Item("Map Field").ToString <> "" Then
                        lstrFilingDateColName = larrDBCol(0).Item("Map Field").ToString
                        sqlBulkCopy.ColumnMappings.Add(lstrFilingDateColName, "ReportDate")
                    End If
                End If

                'If lstrSourceNameColName.Equals("") = False OrElse lstrFilingDateColName.Equals("") = False Then
                '    Dim larrSourceFile() As DataRow
                '    Dim lintMaxPosition As Int64
                '    Dim lintMaxRowIndex As Integer
                '    Dim lintFirstRowIndex As Integer
                '    Dim lCultureInfo As System.Globalization.CultureInfo = Nothing
                '    Dim lenmDateFormatStatus As DateFormatStatus

                '    lenmDateFormatStatus = DateFormatStatus.UK

                '    mdtMapData.Columns.Add("IsSourceFileChecked", GetType(Boolean))

                '    'To Use the date and source of the largest position row from the ePublic.
                '    'If two or more rows share the same position, then use the more recent date and the source off the same row.
                '    For Each lRow As DataRow In mdtMapData.Rows
                '        'Function only when FilingDate has some value and the row is not already processed
                '        If lRow.Item(lstrFilingDateColName) IsNot DBNull.Value AndAlso lRow.Item("IsSourceFileChecked") Is DBNull.Value Then
                '            lRow.Item(lstrFilingDateColName) = lRow.Item("FilingDateCopy")
                '        End If
                '        'Function only when the row is an Investor row and the row is not already processed
                '        If lRow.Item("FundID") IsNot DBNull.Value OrElse lRow.Item("InvestorID") Is DBNull.Value OrElse lRow.Item("IsSourceFileChecked") IsNot DBNull.Value Then Continue For
                '        lstrQuery = "InvestorID='" + lRow.Item("InvestorID").ToString + "'"
                '        larrSourceFile = mdtMapData.Select(lstrQuery)
                '        'Function only if there are multiple rows with the same InvestorID and (FundID is null)
                '        If larrSourceFile.Length > 1 Then
                '            'Find the Index/Position of the first row and consider it the max row initially.
                '            lintFirstRowIndex = mdtMapData.Rows.IndexOf(larrSourceFile(0))
                '            lintMaxPosition = mdtMapData.Rows(lintFirstRowIndex).Item(mstrPosition)
                '            lintMaxRowIndex = lintFirstRowIndex
                '            'Match the positions with each row and see if there is any larger/equal position. If the position is equal then find the index of the row with recent filing date.
                '            For Each lSourceFileRow As DataRow In larrSourceFile
                '                If lSourceFileRow.Item("IsSourceFileChecked") Is DBNull.Value Then
                '                    lSourceFileRow.Item(lstrFilingDateColName) = lRow.Item("FilingDateCopy")
                '                End If
                '                If mdtMapData.Rows.IndexOf(lSourceFileRow) = lintFirstRowIndex Then Continue For
                '                If lSourceFileRow.Item(mstrPosition) = lintMaxPosition Then
                '                    If DateTime.ParseExact(lSourceFileRow.Item(lstrFilingDateColName), CommonCLL.supportedFormats, lCultureInfo, System.Globalization.DateTimeStyles.None) >= DateTime.ParseExact(mdtMapData.Rows(lintMaxRowIndex).Item(lstrFilingDateColName), CommonCLL.supportedFormats, lCultureInfo, System.Globalization.DateTimeStyles.None) Then
                '                        lintMaxRowIndex = mdtMapData.Rows.IndexOf(lSourceFileRow)
                '                        lintMaxPosition = lSourceFileRow.Item(mstrPosition)
                '                    End If
                '                ElseIf lSourceFileRow.Item(mstrPosition) > lintMaxPosition Then
                '                    lintMaxRowIndex = mdtMapData.Rows.IndexOf(lSourceFileRow)
                '                    lintMaxPosition = lSourceFileRow.Item(mstrPosition)
                '                End If
                '            Next

                '            'Update the Source and Filing Date attribute to what the largest position/most recent FilingDate column has.
                '            For Each lSourceFileRow As DataRow In larrSourceFile
                '                lSourceFileRow.Item("IsSourceFileChecked") = True
                '                If mdtMapData.Rows.IndexOf(lSourceFileRow) = lintMaxRowIndex Then Continue For
                '                If lstrSourceNameColName.Equals(("")) = False Then
                '                    lSourceFileRow.Item(lstrSourceNameColName) = mdtMapData.Rows(lintMaxRowIndex).Item(lstrSourceNameColName)
                '                End If
                '                If lstrFilingDateColName.Equals(("")) = False Then
                '                    lSourceFileRow.Item(lstrFilingDateColName) = mdtMapData.Rows(lintMaxRowIndex).Item(lstrFilingDateColName)
                '                End If
                '            Next
                '        End If
                '    Next
                'End If
            End If

            sqlBulkCopy.ColumnMappings.Add(mstrPosition, "Position")
            sqlBulkCopy.ColumnMappings.Add("EntityID", "EntityID")
            sqlBulkCopy.ColumnMappings.Add(mstrMappedInvName, "MappedInvName")
            sqlBulkCopy.ColumnMappings.Add(mstrMappedFundName, "MappedFundName")
            sqlBulkCopy.WriteToServer(mdtMapData)
            sqlBulkCopy.Close()
            sqlBulkCopy = Nothing

            'Import the EDS
            Dim enmResult As DBResult
            Dim lstrErrorMessage As String = ""
            ldtImport = mclsImporteSourceManager.ImportESource(mintEntityID, mintListingID, mintProjectID, mintTemplateID, mintClientID, menmSourceTypeCode, enmResult, lstrErrorMessage)
            pgbImport.IncrementValue(pgbImport.Maximum - pgbImport.Value)
            lblImportProgress.Visible = False
            Me.Cursor = Cursors.Default
            If enmResult = DBResult.SUCCESS Then
                mblnIsImportSuccess = True
                ShowMessageBox(DBResult.EDS_IMPRT_SUCCESS)
                Me.Close()
            ElseIf enmResult = DBResult.EDS_IMPRT_FAIL Then
                Me.ShowMessageBox(enmResult, lstrErrorMessage)
                Exit Sub
            ElseIf enmResult = DBResult.EDS_IMPRT_DUPMATCH_INVLD_MAP Then
                Me.ShowMessageBox(enmResult, lstrErrorMessage)
                Dim lfrmInvalidMappingDetails As New InvalidMappingDetails(ldtImport, mdtMapData)
                lfrmInvalidMappingDetails.mintProjectID = mintProjectID
                lfrmInvalidMappingDetails.Show()
                Exit Sub
            Else
                Me.ShowMessageBox(enmResult)
                Exit Sub
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, mintProjectID)
            pgbImport.Value = 0
        Finally
            lblImportProgress.Visible = False
            larrDBCol = Nothing
            larrMapData = Nothing
            ldtImport = Nothing
            sqlBulkCopy = Nothing
            larrSameFundInNewInv = Nothing
            lblImportProgress.Visible = False
            Me.Cursor = Cursors.Default
        End Try
    End Sub

#End Region

End Class